package deviceinfo.wsm.com.deviceinfo.sim;

import android.content.Context;
import android.os.Build;
import android.telephony.CellLocation;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.text.TextUtils;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

import deviceinfo.wsm.com.deviceinfo.WsmDeviceInfoSDK;
import deviceinfo.wsm.com.deviceinfo.WsmSDK;

/**
 * Created by 王家宝 on 2018/7/5.
 * TEL:18241150668
 * MAIL:639586819@qq.com
 */

public class SimInfoUtils {

    public static LinkedHashMap<String, String> getAll() {
        LinkedHashMap<String, String> listData = new LinkedHashMap<>();
        if (hasSimCard()) {
            listData.put("sim卡状态", getSimCardState() + "");
            listData.put("sim卡IMSI", getSimImsi() + "");
            listData.put("sim卡ICCID", getSimSerial() + "");
            listData.put("sim卡手机号", getSimPhoneNum() + "");
            listData.put("sim卡类型", getSimPhoneType() + "");
            listData.put("sim卡国家码", getSimCountry() + "");
            listData.put("sim卡运营商名称", getSimYName() + "");
            listData.put("sim卡MCC", getSimMCC() + "");
            listData.put("sim卡MNC", getSimMNC() + "");
            listData.put("sim卡LAC", getSimLAC());
            listData.put("sim卡CID", getSimCID());
            CellLocation.requestLocationUpdate();
            listData.put("sim卡纬度", getSimLocationLatitude());
            listData.put("sim卡经度", getSimLocationLongitude());
            listData.put("是否代理", isWifiProxy() + "");

            return listData;
        } else {
            return listData;
        }
    }

    private static boolean isWifiProxy() {

        final boolean IS_ICS_OR_LATER = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;

        String proxyAddress;

        int proxyPort;

        if (IS_ICS_OR_LATER) {

            proxyAddress = System.getProperty("http.proxyHost");

            String portStr = System.getProperty("http.proxyPort");

            proxyPort = Integer.parseInt((portStr != null ? portStr : "-1"));

        } else {

            proxyAddress = android.net.Proxy.getHost(WsmDeviceInfoSDK.getWsmSDK().getAppContext());

            proxyPort = android.net.Proxy.getPort(WsmDeviceInfoSDK.getWsmSDK().getAppContext());

        }

        return (!TextUtils.isEmpty(proxyAddress)) && (proxyPort != -1);

    }


    /**
     * 判断是否包含SIM卡
     *
     * @return 状态
     */
    private static boolean hasSimCard() {
        Context context = WsmDeviceInfoSDK.getWsmSDK().getAppContext();
        TelephonyManager telMgr = (TelephonyManager)
                context.getSystemService(Context.TELEPHONY_SERVICE);
        int simState = telMgr.getSimState();
        boolean result = true;
        switch (simState) {
            case TelephonyManager.SIM_STATE_ABSENT:
                result = false; // 没有SIM卡
                break;
            case TelephonyManager.SIM_STATE_UNKNOWN:
                result = false;
                break;
        }

        return result;
    }

    private static TelephonyManager getService() {
        TelephonyManager telMgr = (TelephonyManager) WsmDeviceInfoSDK.getWsmSDK().getAppContext().getSystemService(Context.TELEPHONY_SERVICE);
        return telMgr;
    }

    private static int getSimCardState() {
        int simState = 0;
        try {
            simState = getService().getSimState();
        } catch (Exception e) {
            simState = -1;
        }
        return simState;
//        switch (simState) {
//            case TelephonyManager.SIM_STATE_ABSENT:
//                // do something
//                break;
//            case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
//                // do something
//                break;
//            case TelephonyManager.SIM_STATE_PIN_REQUIRED:
//                // do something
//                break;
//            case TelephonyManager.SIM_STATE_PUK_REQUIRED:
//                // do something
//                break;
//            case TelephonyManager.SIM_STATE_READY:
//                // do something
//                break;
//            case TelephonyManager.SIM_STATE_UNKNOWN:
//                // do something
//                break;
//        }
    }

    private static String getSimImsi() {
        try {
            return getService().getSubscriberId();
        } catch (Exception e) {
        }
        return "";
    }

    private static String getSimSerial() {
        try {
            return getService().getSimSerialNumber();
        } catch (Exception e) {
        }
        return "";
    }

    private static String getSimPhoneNum() {
        try {
            return getService().getLine1Number();
        } catch (Exception e) {
        }
        return "";
    }

    private static String getSimCountry() {
        try {
            return getService().getNetworkCountryIso();
        } catch (Exception e) {
        }
        return "";
    }

    private static String getSimYName() {
        try {
            return getService().getNetworkOperatorName();
        } catch (Exception e) {
        }
        return "";
    }

    private static String getSimMCC() {
        try {
            return getSimImsi().substring(0, 3);
        } catch (Exception e) {
        }
        return "";
    }

    private static String getSimMNC() {
        try {
            return getSimImsi().substring(3, 5);
        } catch (Exception e) {
        }
        return "";
    }


    private static String getSimLAC() {
        String simLAC = null;
        try {
            switch (getSimPhoneType()) {
                case "CDMA":
                    CdmaCellLocation locationCDMA = (CdmaCellLocation) getService().getCellLocation();
                    simLAC = locationCDMA.getBaseStationId() + "";
                    break;
                case "GSM":
                    GsmCellLocation locationGSM = (GsmCellLocation) getService().getCellLocation();
                    simLAC = locationGSM.getLac() + "";
                    break;
                default:
                    break;
            }

            return simLAC;
        } catch (Exception e) {

        }

        return simLAC;
    }

    private static String getSimCID() {
        String simCID = null;
        try {
            switch (getSimPhoneType()) {
                case "CDMA":
                    CdmaCellLocation locationCDMA = (CdmaCellLocation) getService().getCellLocation();
                    simCID = locationCDMA.getNetworkId() + "";
                    break;
                case "GSM":
                    GsmCellLocation locationGSM = (GsmCellLocation) getService().getCellLocation();
                    simCID = locationGSM.getCid() + "";
                    break;
                default:
                    break;
            }

            return simCID;
        } catch (Exception e) {

        }

        return simCID;
    }

    private static String getSimLocationLatitude() {
        String latitude = null;
        try {
            switch (getSimPhoneType()) {
                case "CDMA":
                    CdmaCellLocation locationCDMA = (CdmaCellLocation) getService().getCellLocation();
                    latitude = locationCDMA.getBaseStationLatitude() + "";
                    break;
                default:
                    break;
            }
            return latitude;

        } catch (Exception e) {

        }
        return latitude;
    }

    private static String getSimLocationLongitude() {
        String longitude = null;
        try {
            switch (getSimPhoneType()) {
                case "CDMA":
                    CdmaCellLocation locationCDMA = (CdmaCellLocation) getService().getCellLocation();
                    longitude = locationCDMA.getBaseStationLongitude() + "";
                    break;
                default:
                    break;
            }
            return longitude;

        } catch (Exception e) {

        }
        return longitude;
    }

    private static String getSimPhoneType() {
        try {
            int type = getService().getPhoneType();
            switch (type) {
                case TelephonyManager.PHONE_TYPE_NONE:
                    return "NONE";
                case TelephonyManager.PHONE_TYPE_GSM:
                    return "GSM";
                case TelephonyManager.PHONE_TYPE_CDMA:
                    return "CDMA";
                case TelephonyManager.PHONE_TYPE_SIP:
                    return "SIP";
                default:
                    return "NONE";
            }
        } catch (Exception e) {
        }
        return "";
    }
}
